<template>
  <header>
    <span @click.stop="$back"><el-icon size="18">
        <Back />
      </el-icon></span>
    <dl>
      <dd v-for="(item, index) in tab" :class="activeindex == index ? 'active' : ''" @click="changewith(index)">
        {{ $t(item) }}
      </dd>
    </dl>
    <strong @click="$openPage('/Account')">{{ $t("language.withdrawal.title3") }}</strong>
  </header>
  <main class="main">
    <div v-if="activeindex == 0">
      <template v-if="vips.currentRank == 0">
        <h3>{{ $t("language.withdrawal.name") }}</h3>
        <ul>
          <li>
            <span>1</span>
            <strong>{{ $t("language.withdrawal.steponetitle") }}</strong>
            <p>
              {{ $t("language.withdrawal.steponetext") }}
            <div class="btnright">
              <van-button @click="$openPage('/vip')">{{ $t('language.withdrawal.btn1') }}</van-button>
              <van-button @click="$openPage('/rechange')">{{ $t('language.withdrawal.btn2') }}</van-button>

            </div>
            </p>
          </li>
          <li>
            <span>2</span>
            <strong>{{ $t("language.withdrawal.steptwetitle") }}</strong>
            <p>
              {{ $t("language.withdrawal.steptwetext") }}
            </p>
          </li>
          <li>
            <span>3</span>
            <strong> {{ $t("language.withdrawal.stepthreetitle") }}</strong>
            <p>
              {{ $t("language.withdrawal.stepthreetext") }}
            </p>
          </li>
          <li>
            <span>4</span>
            <strong> {{ $t("language.withdrawal.stepfourtitle") }}</strong>
            <p>
              {{ $t("language.withdrawal.stepfourtext") }}
            </p>
          </li>
          <li>
            <span>5</span>
            <strong>{{ $t("language.withdrawal.stepfivetitle") }}</strong>
            <p>
              {{ $t("language.withdrawal.stepfivetext") }}
            </p>
          </li>
        </ul>
      </template>
      <template v-else>
        <dl>
          <dd v-for="(item, index) in bar" :class="activeBindex == index ? 'activeB' : ''" @click="coinType(index)">
            <img :src="item.img" alt="" />
            <strong>{{ item.name }}</strong>
          </dd>
        </dl>
        <ol>
          <li>
            <span>{{ $t('language.withdrawal.totalmoney') }}</span><strong>$R {{
      $dot(moneyinfo.coins.totalBalanceCoins)
    }}</strong>
          </li>
          <li><span>{{ $t('language.withdrawal.Withdrawableamount') }}</span><strong>$R {{
      $dot(moneyinfo.coins.allowedWithdrawalCoins) }}</strong></li>
          <li><span>{{ $t('language.withdrawal.Freezeamount') }}</span><strong>$R {{ $dot(moneyinfo.coins.freezeCoins)
              }}</strong></li>
          <li><span>{{ $t('language.withdrawal.Amountindisbursement') }}</span><strong>$R {{
      $dot(moneyinfo.coins.currentProgress)
    }}</strong></li>
        </ol>
        <div class="process">
          <van-progress pivot-color="#0a906c" pivot-text=""
            :percentage="moneyinfo.coins.currentProgress / moneyinfo.coins.totalProgress"
            style="width: 80%;background-color: #fff; height: 20px;border-radius: 10px;" />
          <div><span>{{ $t('language.withdrawal.process') }}<van-icon name="question-o" /></span></div>
        </div>
        <div class="choose">
          <h3>{{ $t('language.withdrawal.chooseAcount') }}</h3>
          <div class="detail">
            <template v-if="banginfo.email && banginfo.mobile">
              <div>
                <span>
                  PIX{{ $t('language.withdrawal.AcountType') }}: CPF
                </span>
                <i @click="openinfo = !openinfo">{{ $t(openinfo ? 'language.withdrawal.up' :
      "language.withdrawal.detail")
                  }}</i>
              </div>
              <span>CPF:{{ banginfo.cpf }}</span>
              <template v-if="openinfo">
                <span>{{ $t('language.withdrawal.detailName') }}:{{ banginfo.name }}</span>
                <span>{{ $t('language.withdrawal.email') }}:{{ banginfo.email }}</span>
                <span>{{ $t('language.withdrawal.phonenumber') }}:{{ banginfo.mobile }}</span>
                <!-- <span>{{ $t('language.withdrawal.cnpj') }}:</span> -->
              </template>
            </template>
            <template v-else>
              <h4>{{ $t('language.withdrawal.noAccount') }}</h4>
            </template>
          </div>
        </div>
        <p>
          <span>{{ $t('language.withdrawal.todayTime') }}/{{ $t('language.withdrawal.money')
            }}: {{ moneyinfo.status.constraint.totalTimesLimit }} |
            {{ moneyinfo.status.constraint.totayAmountLimit }} {{ $t('language.withdrawal.unit') }}</span>
          <van-button @click="$openPage('/vip')">{{ $t('language.withdrawal.uplimit') }}</van-button>
        </p>
        <div class="WithdrawalAmount">
          <h3>{{ $t('language.withdrawal.WithdrawalAmount') }}</h3>
          <dl>
            <!-- <dd>100{{ $t('language.withdrawal.unit') }}</dd>
            <dd>50{{ $t('language.withdrawal.unit') }}</dd> -->
            <dd v-for="item in moneyinfo.configs" @click="chooseAmount(item)"
              :class="getform.configID == item.configID ? 'activemoney' : ''" :key="item.configID">
              {{ item.amount / 100 }} {{ $t('language.withdrawal.unit') }}</dd>
          </dl>

          <span
            :class="currentAmount != '' && Number(currentAmount) / 100 > Number(moneyinfo.coins.allowedWithdrawalCoins) / 100 ? 'error' : ''"
            v-if="currentAmount != '' && Number(currentAmount) / 100 > Number(moneyinfo.coins.allowedWithdrawalCoins) / 100"
            style="padding-top: 5px;display: block;">{{ $t('language.withdrawal.Withdrawallimit') }}</span>
        </div>
        <div class="question">
          {{ $t('language.withdrawal.WithdrawalEncounteredProblems') }}?<span>{{ $t('language.withdrawal.kefu')
            }}</span>
        </div>
        <div class="check">
          <van-checkbox checked-color="#0a906c" v-model="checked">{{ $t('language.withdrawal.again') }}</van-checkbox>
        </div>
        <div class="get">
          <van-button :loading="loading" loading-type="spinner"
            :disabled="loading || (!checked || currentAmount == '' || Number(currentAmount) / 100 > Number(moneyinfo.coins.allowedWithdrawalCoins) / 100)"
            @click="getmoeny" class="btn">{{
      $t('language.withdrawal.Withdrawal') }}</van-button>
        </div>
        <!-- <div class="tips">
          {{ $t('language.withdrawal.you') }} <span>
            {{ $t('language.withdrawal.cartBag') }}</span>{{ $t('language.withdrawal.goin') }}
        </div> -->
      </template>
    </div>
    <template v-if="activeindex == 1">
      <dl class="record" v-if="recordList.list.length != 0">
        <dd v-for="(item, index) in recordList.list" :key="index">
          <p class="time">{{ item.date }}</p>
          <div class="record-item" v-for="child in item.data " :key="child.orderID">
            <div>
              <p>{{ $t('language.rechange.orderID') }}: {{ child.orderID }}</p>
              <i @click="copyToClipboard(child.orderID, proxy)">{{ $t('language.rechange.copy') }}</i>
            </div>
            <span>{{ $t('language.withdrawal.WithdrawalAmount') }}: <strong>{{ child.orderAmount / 100
                }}R$</strong></span>
            <div class="status">
              <strong>{{ child.orderDate }}</strong>
              <span v-if="child.orderStatus == 0">{{ $t('language.rechange.waitpay') }}</span>
              <span v-else style="color: #0c730c;">{{ $t('language.rechange.paysuccess') }}</span>

            </div>
          </div>
        </dd>
      </dl>
      <div v-if="recordList.list.length == 0 && !withdrawalload" class="nodata">
        <img src="@/assets/img/bg4.png" alt="" />
        <p>{{ $t("language.rechange.tips") }}</p>
      </div>
    </template>

  </main>
</template>
<script setup lang="ts">
import { useI18n } from "vue-i18n";
import { copyToClipboard } from "@/utils/common";
import { reactive, ref, watch, getCurrentInstance } from "vue";
import { useStore } from "vuex";
const store = useStore();
let activeindex = ref(0);
let info = JSON.parse(localStorage.getItem("info"));
const { proxy } = getCurrentInstance();
const getImg = (url) => {
  return new URL(`../../../assets/img/${url}`, import.meta.url).href;
};
const { t } = useI18n();
//详情
let openinfo = ref(false)
let checked = ref(false)
//切换金币类型
let activeBindex = ref(0);
let bar = reactive([
  {
    img: getImg("mylrft.png"),
    name: "BRL",
  },
  {
    img: getImg("t1.png"),
    name: "USDT",
  },
]);
//获取提现记录

let record = reactive({
  id: info.gameID,
  pageIndex: 1,
  pageSize: 10
})
let recordList = reactive({
  list: [],
  pageIndex: 1,
  pageSize: 11,
  totalCount: 23
})

let withdrawalRecord = () => {

  proxy.$post('/Payment/v1.0/Web/User/Withdrawal/List', record).then(({ data }) => {
    console.log(data, 'uiuiui');
    switch (data.code) {
      case 0:
        for (let key in data.data) {
          recordList[key] = data.data[key];
        }
        break;
    }
  }).finally(() => {
    let time = setInterval(() => {
      ElLoading.service({
        lock: true,
        background: 'rgba(0, 0, 0, 0.7)',
        customClass: 'el-loading'
      }).close()
      withdrawalload.value = false
      clearInterval(time)
    }, 300)
  })
}
watch(() => activeindex.value, (newval) => {
  if (newval == 1) {
    withdrawalload.value = true
    ElLoading.service({
      lock: true,
      background: 'rgba(0, 0, 0, 0.7)',
      customClass: 'el-loading'
    })
    withdrawalRecord()
  }
})
let changewith = (index) => {
  activeindex.value = index
}
//选择提现金额
let getform = reactive({
  id: info.gameID,
  accountType: 0,
  account: "543254353",//银行卡账户
  configID: 0
})
let currentAmount = ref("")
let chooseAmount = (item) => {
  getform.configID = item.configID
  currentAmount.value = item.amount
}
//金额信息
let moneyinfo = reactive({
  coins: {
    allowedWithdrawalCoins: 400,
    currentProgress: 0,
    freezeCoins: 0,
    grantCoins: 0,
    totalBalanceCoins: 4330,
    totalProgress: 20000,
  },
  configs: [],
  status: {
    constraint: {
      totalTimesLimit: 0,
      totayAmountLimit: 0
    },
    cpf: "",
    pixType: 0,
  }
})
// 用户绑定信息
let banginfo = reactive({
  gameID: 0,
  surname: "",
  name: "",
  mobile: "",
  email: "",
  type: 0,
  cpf: ""
})
//获取vip等级
let vips = reactive({
  currentRank: 0,
  currentVipRecharge: 0,
  id: 0,
  nextRank: 0,
  upgradeNeedRecharge: 0,
})
// 数据初始化
let withdrawalload = ref(true)
let initData = () => {
  withdrawalload.value = true
  ElLoading.service({
    lock: true,
    background: 'rgba(0, 0, 0, 0.7)',
    customClass: 'el-loading'
  })
  Promise.all([
    proxy.$post(`/UserCenter/v1.0/User/Vip`, { id: info.gameID }),//获取vip信息金额
    proxy.$post(`/Payment/v1.0/User/Withdrawal/Protocol`, { id: info.gameID }),//获取取款信息金额
    proxy.$post(`/Payment/v1.0/Web/Account/Info`, { id: info.gameID })//获取用户绑定信息
  ]).then(([vip, money, userinfo]) => {
    activeBindex.value = 0
    for (let key in vip.data.data) {
      vips[key] = vip.data.data[key]
    }
    for (let key in money.data.data) {
      moneyinfo[key] = money.data.data[key]
    }
    for (let key in userinfo.data.data) {
      banginfo[key] = userinfo.data.data[key]
    }
  }).finally(() => {
    let time = setInterval(() => {
      ElLoading.service({
        lock: true,
        background: 'rgba(0, 0, 0, 0.7)',
        customClass: 'el-loading'
      }).close()
      withdrawalload.value = false
      clearInterval(time)
    }, 300)

  })
}

initData()

// 切换取款信息金额
let coinType = (index) => {
  activeBindex.value = index
  proxy.$post(`/Payment/v1.0/User/Withdrawal/Protocol`, { id: info.gameID }).then(({ data }) => {
    console.log(data, 'ddd');
    moneyinfo.coins = data.data.coins
    moneyinfo.configs = data.data.configs
    console.log(data.data.configs, 99);
    moneyinfo.status = data.data.status
  })
}

//取款
let loading = ref(false)
let getmoeny = () => {
  if (checked.value) {
    loading.value = true
    ElLoading.service({
      lock: true,
      background: 'rgba(0, 0, 0, 0.7)',
      customClass: 'el-loading'
    })
    proxy.$post("/Payment/v1.0/Web/User/Withdrawal", { ...getform }).then(({ data }) => {
      ElMessage.success({
        message: t("language.withdrawal.getsuccess"),
        type: "success",
        duration: 1000,
        onClose() {
          store.dispatch('GetUserInfo', { gameID: info.gameID }).then(({ data }) => {
            if (data.code == 0) {
              store.commit('SET_COININFO', data.data.accounts)
              localStorage.setItem('coininfo', JSON.stringify(data.data.accounts))
            }
          })
        }
      });
    }).finally(() => {

      loading.value = false
      let time = setInterval(() => {
        ElLoading.service({
          lock: true,
          background: 'rgba(0, 0, 0, 0.7)',
          customClass: 'el-loading'
        }).close()
        activeindex.value = 1
        clearInterval(time)
      }, 300)

    })
  } else {
    ElMessage.error({
      message: t("language.withdrawal.read"),
      type: "success",
      duration: 1000,
    });
  }

}
let tab = reactive([
  "language.withdrawal.title1",
  "language.withdrawal.title2",
]);
</script>
<style scoped lang="scss">
header {
  width: 100%;
  z-index: 88;
  position: fixed;
  left: 0;
  top: 0;
  @include flex(row, space-between, flex-end);
  background-color: rgb(36, 39, 43);
  padding: 30px 20px 9px;
  color: #fff;

  span {
    display: block;
    height: 30px;
    width: 30px;
    @include flex(row, center, center);
    border: 2px solid #fff;
    border-radius: 16px;

    img {
      width: 100%;
      height: 100%;
    }
  }

  strong {
    text-align: right;
    display: block;
  }

  dl {
    flex: 1;
    @include flex(row, center, center);

    dd {
      color: #8e8e8c;
      font-size: 18px;
      position: relative;
    }

    .active {
      color: #fff;

      &::after {
        content: "";
        position: absolute;
        width: 60%;
        bottom: -4px;
        height: 2px;
        background-color: #fff;
        margin-left: -30%;
        left: 50%;
      }
    }

    dd+dd {
      margin-left: 20px;
    }
  }
}

.main {
  background-color: #1a1a1a;
  padding: 70px 0px 0px;
  width: 100%;
  min-height: 100vh;
  background-color: rgb(26, 26, 26);

  >div {
    padding: 10px 20px 30px;

    h3 {
      font-size: 19px;
      color: #b5bac8;
      text-align: center;
      margin-bottom: 20px;
    }

    ul {
      width: 100%;

      >li:first-child {
        span {
          background-color: #5b5b5b;
          color: #fff;
        }
      }

      li {
        border-left: 2px solid #fff;
        padding-left: 20px;
        position: relative;

        strong {
          font-size: 16px;
          color: #fff;
          display: block;
          margin-bottom: 8px;
        }

        p {
          font-size: 12px;
          color: #c0c4cc;
          line-height: 20px;
          padding-bottom: 40px;

          >.btnright {
            width: 100%;
            height: 40px;
            margin-top: 10px;
            @include flex(row, flex-end, center);

            .van-button {
              font-size: 12px;
              background: transparent;
              padding: 7px 15px;
              color: #0a906c;
              height: 32px;
              border: 1px solid #0a906c;
              border-radius: 5px;
              display: block;
            }

            .van-button+.van-button {
              margin-left: 10px;
              background-color: #0a906c;
              color: #fff;
            }

          }
        }

        >span {
          display: block;
          position: absolute;
          width: 24px;
          height: 24px;
          border-radius: 12px;
          background-color: #fff;
          text-align: center;
          line-height: 24px;
          font-size: 14px;
          color: #666;
          left: -12px;
        }
      }

      li:last-child {
        border: none;
      }
    }

    >dl {
      height: 44px;
      width: 100%;
      @include flex(row, flex-start, center);

      dd {
        height: 44px;
        @include flex(row, flex-start, center);
        border-bottom: 2px solid transparent;

        img {
          width: 24px;
          margin-right: 5px;
        }

        strong {
          color: #b5bac8;
          font-size: 17px;
        }
      }

      .activeB {
        strong {
          color: #0a906c;
        }

        border-bottom: 2px solid #0a906c;
      }

      dd+dd {
        margin-left: 20px;
      }
    }

    ol {
      width: 100%;
      @include flex(row, space-between, flex-start);
      flex-wrap: wrap;
      margin-top: 10px;

      li {
        width: 50%;
        background-color: rgb(50, 53, 62);
        min-height: 102px;
        @include flex(column, center, center);
        margin-bottom: 5px;

        span {
          color: #b5bac8;
          font-size: 18px;
          text-align: center;
        }

        strong {
          font-size: 20px;
          color: #fff;
        }
      }

      li:nth-child(2n) {
        border-left: 1px solid rgb(100, 107, 107);
      }
    }

    .process {
      width: 50%;
      min-height: 50px;
      background-color: rgb(50, 53, 62);
      padding: 5px 0;
      @include flex(column, center, center);
      border-right: 1px solid rgb(100, 107, 107);

      >div {
        width: 100%;
        margin-top: 8px;
        padding: 0 20px;
        @include flex(row, center, center);

        span {
          text-align: center;
          width: 100%;
          color: #b5bac8;
        }
      }
    }

    .choose {
      h3 {
        text-align: left;
        color: #fff;
        margin: 30px 0 8px 0;
      }

      h4 {
        text-align: center;
        height: 40px;
        line-height: 40px;
        color: #fff
      }

      .detail {
        width: 100%;
        padding: 15px 18px;
        background-color: rgb(35, 38, 43);
        border-radius: 10px;

        >div {
          @include flex(row, space-between, center);

          span {
            color: #fff;
            font-size: 16px;
          }

          i {
            font-size: 14px;
            color: #0a906c;
            font-style: normal;
          }
        }

        span {
          color: #fff;
          font-size: 19px;
          display: block;
          margin-top: 5px;
        }
      }
    }

    >p {
      @include flex(row, space-between, center);
      margin: 10px 0 0 0;

      span {
        color: #b5bac8;
      }

      .van-button {
        padding: 8px 5px;
        background-color: transparent;
        color: #0a906c;
        border: 1px solid #0a906c;
      }
    }

    .WithdrawalAmount {
      margin-top: 20px;
      width: 100%;

      h3 {
        text-align: left;
        color: #fff;
        font-size: 19px;
      }

      dl {
        width: 100%;
        @include flex(row, flex-start, center);
        flex-wrap: wrap;

        dd {
          width: 110px;
          padding: 10px 0;
          text-align: center;
          color: #fff;
          border-radius: 10px;
          border: 2px solid rgb(73, 77, 83);
          margin: 10px 15px 0 0;
        }

        .activemoney {
          border: 2px solid #0a906c;
          background-color: #474a51;
        }


      }

      .error {
        color: red;
      }

    }

    .question {
      text-align: center;
      color: #b5bac8;
      margin-top: 20px;

      span {
        color: #fff;
      }
    }
  }

  .check {
    margin-top: 30px;

    :deep(.van-checkbox__label) {
      color: #b5bac8;
    }

  }

  .get {
    margin-top: 10px;

    .btn {
      width: 100%;
      background-color: #0a906c;
      border: none;
      height: 40px;
      color: #fff;
      margin: 0;

    }
  }

  .tips {
    padding-left: 20px;
    margin-top: 10px;
    color: #fff;

    span {
      color: #0a906c;
    }
  }

  .record {
    width: 100%;

    dd {
      width: 100%;

      >p {
        height: 40px;
        width: 100%;
        padding: 0 16px;
        line-height: 40px;
        font-size: 14px;
        background-color: #40434c;
        color: #b5bac8;
      }

      .record-item {
        padding: 16px;

        >div {
          @include flex(row, space-between, center);

          p {
            color: #fff;
            font-size: 14px;
          }

          i {
            font-style: normal;
            color: #0a906c;
            font-size: 15px;
          }
        }

        >span {
          font-size: 14px;
          color: #fff;
          display: block;
          margin: 6px 0;
        }

        .status {
          @include flex(row, space-between, center);

          strong {
            color: #666;
            font-weight: normal;
          }

          span {
            font-size: 14px;
            color: #f9e009;
          }
        }
      }

      .record-item+.record-item {
        border-top: 1px solid #40434c;
      }
    }

  }

  .nodata {
    width: 100%;
    height: 400px;

    img {
      width: 200px;
    }

    @include flex(column, center, center);

    p {
      font-size: 17px;
      color: #b5bac8;
      margin-top: 50px;
    }
  }
}
</style>
